﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;

namespace GUI
{
    using GUI.Library;
    using SV;

    public partial class FrmInput : Form
    {
        public FrmInput()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Access to catalog
        /// </summary>
        protected SV.Catalog _bllCat;

        /// <summary>
        /// Access to catalog
        /// </summary>
        protected SV.Bussiness _bllBus;

        /// <summary>
        /// Default table empty
        /// </summary>
        protected DataTable _dtb = new DataTable("Tmp");

        /// <summary>
        /// Form state
        /// </summary>
        public State _state;

        /// <summary>
        /// Change status command button on form
        /// </summary>
        /// <param name="isEnable">Enable</param>
        protected void ChangeStatus(bool isEnable = true)
        {
            btnAdd.Enabled = isEnable;
            btnEdit.Enabled = isEnable;
            btnDelete.Enabled = isEnable;

            btnSave.Enabled = !isEnable;
            btnCancel.Enabled = !isEnable;

            btnRefresh.Enabled = isEnable;
            //bbiFind.Enabled = isEnable;
            btnPrint.Enabled = isEnable;
        }

        #region Virtual
        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected virtual void PerformAdd()
        {
            _state = State.Add;
            ChangeStatus(false);
            ReadOnlyControl(false);
            //ClearDataBindings();
            ResetInput();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected virtual void PerformEdit()
        {
            _state = State.Edit;
            ChangeStatus(false);
            ReadOnlyControl(false);
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        protected virtual void PerformDelete() { }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected virtual void PerformSave()
        {

        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected virtual void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            ReadOnlyControl();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected virtual void PerformRefresh()
        {
            _state = State.View;
        }

        /// <summary>
        /// Perform when click find button
        /// </summary>
        protected virtual void PerformFind() { }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected virtual void PerformPrint() { }

        /// <summary>
        /// Perform when click collapse button
        /// </summary>
        protected virtual void PerformCollapse() { }

        /// <summary>
        /// Perform when click expand button
        /// </summary>
        protected virtual void PerformExpand() { }

        /// <summary>
        /// Set null value prompt
        /// </summary>
        protected virtual void SetNullPrompt() { }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected virtual void ResetInput() { }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected virtual void ClearDataBindings() { }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected virtual void DataBindingControl() { }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only</param>
        protected virtual void ReadOnlyControl(bool isReadOnly = true) { }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns></returns>
        protected virtual bool UpdateObject() { return true; }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns></returns>
        protected virtual bool InsertObject() { return true; }

        /// <summary>
        /// Load data
        /// </summary>
        protected virtual void LoadData() { }

        /// <summary>
        /// Valid data before insert or update to database
        /// </summary>
        /// <returns></returns>
        protected virtual bool ValidInput() { return true; }

        /// <summary>
        /// System timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void TimerTick(object sender, EventArgs e) { }
        #endregion

        private void FrmInput_Load(object sender, EventArgs e)
        {
            ChangeStatus();
            SetNullPrompt();
            PerformRefresh();
        }

        private void bmgMain_ItemClick(object sender, ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "btnAdd":
                    PerformAdd();
                    break;

                case "btnEdit":
                    PerformEdit();
                    break;

                case "btnDelete":
                    PerformDelete();
                    break;

                case "btnSave":
                    PerformSave();
                    break;

                case "btnCancel":
                    PerformCancel();
                    break;

                case "btnRefresh":
                    PerformRefresh();
                    break;

                case "btnFind":
                    PerformFind();
                    break;

                case "btnCollapse":
                    PerformCollapse();
                    break;

                case "btnExpand":
                    PerformExpand();
                    break;

                case "btnPrint":
                    PerformPrint();
                    break;

                default:
                    Close();
                    break;
            }
        }
    }
}